package cn.wizzer.app.web.modules.controllers.platform.user.account;

import cn.wizzer.app.user.modules.models.UserWithdraw;
import cn.wizzer.app.user.modules.models.constant.WithdrawOPType;
import cn.wizzer.app.user.modules.models.enums.WithdrawChannel;
import cn.wizzer.app.user.modules.models.enums.WithdrawStatusEnum;
import cn.wizzer.app.user.modules.services.account.UserWithdrawService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.math.BigDecimal;
import java.util.Map;

@IocBean
@At("/platform/user/account/withdraw")
public class WithdrawController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserWithdrawService withdrawService;

    @At("")
    @Ok("beetl:/platform/user/account/withdraw/index.html")
    @RequiresPermissions("user.account.withdraw")
    public void index() {
    }

    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.account.withdraw.add")
    @SLog(tag = "提现", msg = "申请为用户:${args[0]}提现:${args[1]}")
    public Object addDo(@Param("toUser") String toUser,
                        @Param("amount") BigDecimal amount,
                        @ParamVerify(required = false) String cardId,
                        @ParamVerify(required = true) String currencyId,
                        @ParamVerify(required = false) WithdrawChannel channel) throws Exception {
        withdrawService.manual(currencyId, amount, toUser, cardId, StringUtil.getPlatformUid(), channel);
        return Result.success();
    }

    @At("/examine")
    @Ok("json")
    @RequiresPermissions("user.account.withdraw.examine")
    @SLog(tag = "确认提现", msg = "确认转发${args[0]}")
    public Object examine(@Param("id") String id, String remark, @Param("sure") Boolean sure, String opType, WithdrawChannel channel) throws Exception {
        UserWithdraw withdraw = withdrawService.fetch(id);
        // 审批时status只有是APPLYING才可以
        if (WithdrawOPType.EXAMINE.equals(opType) && !withdraw.getStatus().equals(WithdrawStatusEnum.APPLYING)) {
            return Result.error("订单不是APPLYING状态，刷新页面重试");
        }
        // 复审时status只有是PENDING_BANK才可以
        else if (WithdrawOPType.REVIEW.equals(opType) && !withdraw.getStatus().equals(WithdrawStatusEnum.PENDING_BANK)) {
            return Result.error("订单不是PENDING_BANK状态，刷新页面重试");
        }
        // 不同地址请求提现，需要走不同的回调路径，不然无法回调
        String requestURL = Mvcs.getReq().getRequestURL().toString();
        withdrawService.examine(id, remark, sure, channel, StringUtil.getPlatformUid(), requestURL);
        return Result.success();
    }


    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.withdraw")
    public Object data(@Param("conditionData") String conditionData,
                       @Param("currencyId") String currencyId,
                       @Param("status") String status,
                       @Param("searchDate") String[] searchDate,
                       @Param("searchKeyword") String searchKeyword,
                       @Param("channel") WithdrawChannel channel,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Pagination pagination = withdrawService.listPage(conditionData, currencyId, status, searchDate, searchKeyword, channel, pageNumber, pageSize);
        // 统计提现总额，根据搜索条件
        Map<String, BigDecimal> sum = withdrawService.searchSum(conditionData, currencyId, status, searchDate, searchKeyword, channel);
        return Result.success().addData(Result.createMap("pagination", pagination, "sum", sum));
    }

    /**
     * 获取提现方式
     *
     * @param bankLibId 银行id
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.withdraw")
    public Object getWithdrawalMethod(@Param("bankLibId") String bankLibId) {
        return Result.success(withdrawService.getWithdrawalMethod(bankLibId));
    }

    /**
     * 提现状态枚举
     *
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.account.list")
    public Object status() {
        return Result.success(WithdrawStatusEnum.toList());
    }


}
